package com.zust.ysc012.service;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;

import static com.zust.ysc012.dao.PublicDao.select_number_sql;
import static com.zust.ysc012.dao.PublicDao.select_sign_sql;
import static com.zust.ysc012.dao.StudentDao.*;
import static com.zust.ysc012.dao.TeacherDao.*;

public class EnterService {
    public static int getSign(String account) throws SQLException {
        int i;
        i = select_sign_sql(account);
        return i;
    }

    public static boolean isCorrect(String account, String password, int i) throws SQLException {
        if (i == 1) {
            if (password.equals(select_password_student_sql(account))) {
                return true;
            } else {
                return false;
            }
        } else {
            if (password.equals(select_password_teacher_sql(account))) {
                return true;
            } else {
                return false;
            }
        }
    }
    public static boolean isRegister(String account, String password) throws SQLException {
        if (select_number_sql(account)) {
            int i = select_sign_sql(account);
            if (i == 1) {
                insert_student_account_sql(account, password);
            } else {
                insert_teacher_account_sql(account, password);
            }
            return true;
        } else {
            return  false;
        }
    }

    public static String getUserName(String account) throws SQLException, UnsupportedEncodingException {
        int i = select_sign_sql(account);
        String name;
        if (i == 1) {
            name = select_student_name_sql(account);
        } else {
            name = select_teacher_name_sql(account);
        }
        return name;
    }
}
